package my.study.c.consul.provider.impl.app.configuration;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import my.study.common.util.TimeUtil;
import org.apache.ibatis.reflection.MetaObject;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@MapperScan("my.study.c.consul.provider.impl.app.*.mapper*")
public class MybatisPlusConfig {

    // 最新版
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }




    @Component
    class MybatisPlusFillConfig implements MetaObjectHandler {

        @Override
        public void insertFill(MetaObject metaObject) {
            setFieldValByName("createTime", TimeUtil.time(), metaObject);
            setFieldValByName("updateTime", TimeUtil.time(), metaObject);
        }

        @Override
        public void updateFill(MetaObject metaObject) {
            setFieldValByName("updateTime", TimeUtil.time(), metaObject);
        }
    }

}
